import datetime
import os
import time

import numpy as np
from matplotlib import pyplot as plt
from numpy import double


class BatteryRatioParser:
    def __init__(self, data_path):
        self.data = [0] * 11
        self.data_path = data_path


    def parse(self):
        for file_name in os.listdir(self.data_path):
            print(file_name + ": ")
            self.parse_file(os.path.join(self.data_path, file_name))


    def parse_file(self, filePath):
        f = open(filePath)
        list = [0] * 11
        self.dataset = np.loadtxt(filePath,delimiter=',')
        self.add_data(list)


    def add_data(self, addList):
        for i in range(11):
            self.data[i] = self.data[i] + addList[i]

    def plot(self, top):
        # 电流

        time = [datetime.datetime.fromtimestamp(x/1000) for x in self.dataset[:,0]]
        print(time)
        print(time)
        ax = plt.figure().add_subplot(111)
        ax2 = plt.twinx()
        ax2.set_ylabel(r"Capacity (/mAh)")
        ax2.plot(time,self.dataset[:,1]/1000,label="Capacity")
        ax2.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
        # 种类
        for index in range(2, top if (self.dataset[0].size - 1) > top else (self.dataset[0].size)):
            ax.plot(time,self.dataset[:,index],label="Category "+str(index))
        ax.set_xlabel("Time", fontproperties='Times New Roman', fontsize=12)
        ax.set_ylabel("Delay Ratio", fontproperties='Times New Roman', fontsize=12)
        ax.legend(bbox_to_anchor=(1.05, 0), loc=3, borderaxespad=0)
        plt.show()

if __name__ == '__main__':
    parser = BatteryRatioParser("../../data/BatteryRatioHistory")
    parser.parse()
    parser.plot(7)